﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System.Collections.Specialized
Imports System.Text
Imports System.Web.HttpUtility

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A helper class to create a URL with a query string
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class URLBuilder
        Inherits NameValueCollection

        Private strBaseURL As String

        ''' <summary>
        ''' Creates a new URL Builder object with a base URL
        ''' </summary>
        ''' <param name="strBaseURL"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strBaseURL As String)
            Me.strBaseURL = strBaseURL
        End Sub

        ''' <summary>
        ''' Returns the URL complete with the querystring
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function ToString() As String
            Dim sbString As New StringBuilder()

            For Each strKey As String In MyBase.Keys
                If Not String.IsNullOrEmpty(MyBase.Item(strKey)) Then
                    For Each strValue In MyBase.Item(strKey).Split(CChar(","))
                        Dim strSymbol As String
                        If sbString.Length = 0 Then
                            strSymbol = "?"
                        Else
                            strSymbol = "&"
                        End If
                        sbString.Append(String.Format("{0}{1}={2}", strSymbol, UrlEncodeUnicode(strKey), UrlEncodeUnicode(strValue)))
                    Next
                End If
            Next

            Return String.Format("{0}{1}", strBaseURL, sbString.ToString())
        End Function
    End Class

End Namespace
